package com.itheima.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.youdianda.common.ResultData;
import com.itheima.youdianda.entity.SysConfig;
import com.itheima.youdianda.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;



@RestController
@RequestMapping("/sys_config")
@CrossOrigin
@Slf4j
public class SysConfigController {
    @Autowired
    private SysConfigService sysConfigService;

    @GetMapping("/all")
    public ResultData all() {
        //1. 查询组件名称
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.groupBy(SysConfig::getTabValue);
        List<SysConfig> sysConfigs = sysConfigService.list(queryWrapper);
        List<String> tabValues = new ArrayList<>();
        List<String> tableValues = sysConfigs.stream().map(sysConfig -> {
            return sysConfig.getTabValue();
        }).collect(Collectors.toList());

        //2.根据组件名称查询子类数据
        List<SysConfig> list = sysConfigService.list();
        //3.创建三个空集合
        List<SysConfig> baseConfigs = new ArrayList<>();
        List<SysConfig> systemConfigs = new ArrayList<>();
        List<SysConfig> customConfigs = new ArrayList<>();
        for (SysConfig sysConfig : list) {
            if ("base".equals(sysConfig.getTabValue())) {
                baseConfigs.add(sysConfig);
            } else if ("system".equals(sysConfig.getTabValue())) {
                systemConfigs.add(sysConfig);
            } else {
                customConfigs.add(sysConfig);
            }
        }

        //4.将base集合按名称拆分成3个集合
        List<List<SysConfig>> baseConfigGroupList = new ArrayList<>();
        baseConfigs.stream().collect(Collectors.groupingBy(SysConfig::getGroupValue, Collectors.toList())).forEach((model, configList) -> {
            baseConfigGroupList.add(configList);
        });
        List baseChildrenList = new ArrayList();
        for (List<SysConfig> sysConfigList : baseConfigGroupList) {
            Map map = new HashMap();

            if (sysConfigList.get(0).getGroupValue().equals("站点配置")) {
                List list1 = new ArrayList();
                Map map1 = new HashMap();
                Map map2 = new HashMap();
                map1.put("label", "是");
                map1.put("value", "1");
                map2.put("label", "否");
                map2.put("value", "2");
                list1.add(map1);
                list1.add(map2);
                for (SysConfig sysConfig : sysConfigList) {
                    if (sysConfig.getAttrKey().equals("close_website")) {
                        sysConfig.setConfigValue(list1.toString());
                        break;
                    }
                }

                map.put("name", "站点配置");
                map.put("data", sysConfigList);
                baseChildrenList.add(map);
            } else if (sysConfigList.get(0).getGroupValue().equals("底部信息")) {
                map.put("name", "底部信息");
                map.put("data", sysConfigList);
                baseChildrenList.add(map);
            } else {
                map.put("name", "其他配置");
                map.put("data", sysConfigList);
                baseChildrenList.add(map);

            }
        }

        List list1 = new ArrayList();
        list1.add(baseChildrenList.get(1));
        list1.add(baseChildrenList.get(2));
        list1.add(baseChildrenList.get(0));


        //4.将system集合按名称拆分成集合
        List systemChildrenList = new ArrayList();
        Map map = new HashMap();
        map.put("name", systemConfigs.get(0).getGroupValue());
        map.put("data", systemConfigs);
        systemChildrenList.add(map);

        //5.将custom集合拆分
        List<List<SysConfig>> customConfigGroupList = new ArrayList<>();
        customConfigs.stream().collect(Collectors.groupingBy(SysConfig::getGroupValue, Collectors.toList())).forEach((model, configList) -> {
            customConfigGroupList.add(configList);
        });

        List customChildrenList = new ArrayList();
        for (List<SysConfig> sysConfigList : customConfigGroupList) {
            Map map1 = new HashMap();
            if (sysConfigList.get(0).getGroupValue().equals("前台图片")) {
                map1.put("name", "前台图片");
                map1.put("data", sysConfigList);
                customChildrenList.add(map1);
            } else if (sysConfigList.get(0).getGroupValue().equals("通知公告")) {
                map1.put("name", "通知公告");
                map1.put("data", sysConfigList);
                customChildrenList.add(map1);
            }
        }

        //6.
        Map baseMap = new HashMap();
        Map systemMap = new HashMap();
        Map customMap = new HashMap();

        baseMap.put("name", "基础配置");
        baseMap.put("key", "base");
        baseMap.put("children", list1);

        systemMap.put("name", "系统设置");
        systemMap.put("key", "system");
        systemMap.put("children", systemChildrenList);

        customMap.put("name", "其他设置");
        customMap.put("key", "custom");
        customMap.put("children", customChildrenList);

        //7.
        List firstList = new ArrayList();
        firstList.add(baseMap);
        firstList.add(systemMap);
        firstList.add(customMap);

        //8.创建最大的map
        Map returnMap = new HashMap();
        returnMap.put("list", firstList);


        //9.查询allData
        QueryWrapper<SysConfig> configQueryWrapper = new QueryWrapper<>();
        configQueryWrapper.select("id", "attr_key", "attr_value", "validator", "config_value", "type");
        List<SysConfig> sysConfigList = sysConfigService.list(configQueryWrapper);
        for (SysConfig sysConfig : sysConfigList) {
            if (StringUtils.isNotEmpty(sysConfig.getConfigValue())) {
                log.info(sysConfig.getConfigValue());
                String configValue = sysConfig.getConfigValue();
                String[] split = configValue.split("&");
                List<Map> configVal = new ArrayList<>();
                for (String s : split) {
                    Map map1 = new HashMap();
                    String[] split1 = s.split("=");
                    map1.put("label", split1[0]);
                    map1.put("value", split1[1]);
                    configVal.add(map1);
                }

                sysConfig.setConfigValue(configVal.toString());
            }
        }
        returnMap.put("allData", sysConfigList);

        //10.查询confimForm
        QueryWrapper<SysConfig> wrapper = new QueryWrapper<>();
        configQueryWrapper.select("attr_key", "attr_value");
        List<SysConfig> configs = sysConfigService.list(wrapper);
        Map map1 = new HashMap();
        for (SysConfig config : configs) {
            map1.put(config.getAttrKey(), config.getAttrValue());
        }
        returnMap.put("configForm", map1);

        //11.查询sit name
        Map map2 = new HashMap();
        for (SysConfig sysConfig : list) {
            if (sysConfig.getAttrKey().equals("site_name")) {
                map2.put("site_name", sysConfig.getValidator());
                break;
            }
        }
        returnMap.put("configRules", map2);

        return ResultData.ok("查询成功", returnMap);

    }

    @PostMapping("/update")
    public ResultData update(@RequestBody List<SysConfig> sysConfigs) {
        sysConfigService.updateBatchById(sysConfigs);
        return ResultData.ok("修改成功", "");
    }
}
